
addpath('gpml-matlab-v3.1-2010-09-27');
addpath('Netlab');
startup;

gendata([0.6;0.6]);
dim = 2; % for ISO
n_expts = 5;

func = @covSEiso;
D = load('gp_data'); % much of this is CVed from cross_validate.m
dim = size(D);
n = size(D,1); ntot=n;
D = D(1:ntot,:); % throw away data if need be
dim = dim(2)-1; % i.e. data are from a dim-dimensional space; the -1 is not counting the class label
n_class = max(D(:,dim+1));

X = D(:,1:dim);
y1 = D(:,dim+1);
y = reshape(kron(ones(n,1),1:n_class),n*n_class,1)==repmat(y1,n_class,1); % expressed as 1-of-n_class encoding

vals = zeros(n_expts, 2*dim*n_class);
hypses = zeros(n_expts, dim*n_class);

K = zeros(n,n,n_class);sigma_noise = 1e-7;
hyps = load('gp_hyps')'; hyps = reshape(hyps, 1, n_class*dim);
for c = 1:n_class
  K(:,:,c) = func(hyps(c*2-1:c*2), X, X) + sigma_noise*eye(n); % hax
end  
approxF = alg_3_3(n, n_class, K, y);
dels = zeros(n_expts, dim*n_class);

for gci = 1:n_expts
  hyps = repmat([0.2 * (0.5 * (n_expts + 1) - gci); 0], n_class, 1)';
 % gendata([0;0]);
  hypses(gci,:) = hyps;
  fprintf('Performing gradchek %i\n', gci);
  [grad, del] = gradchek(hyps, @val_3_44, @grad_3_44, func, n, n_class, X, y, approxF);
  vals(gci, :) = [grad, grad-del];
  dels(gci, :) = del;
end
clf;
hist(dels(:));
dlmwrite('conf_errors', dels);
dlmwrite('conf_results', vals); % thus this is of form [ analytics experiments ]
dlmwrite('conf_hyps', hypses); % remember this stores them by row